﻿using EMall.Domain;
using System.Text.Json;

namespace EMall.ProductService.Categories
{
    public class Category : AggregateRoot<int>
    {
        public string Name { get; private set; }
        public string Code { get; private set; }
        public int ParentId { get; private set; }
        protected Category() { }
        public Category(string name, string code, int parentId)
        {
            Name = name;
            ParentId = parentId;
            Code = code;
            AddDomainEvent(new EntityCreatedDomainEvent<Category>(this));
        }
        public void Update(string name, string code, int parentId)
        {
            var originJson = JsonSerializer.Serialize(this);
            Name = name;
            ParentId = parentId;
            Code = code;
            AddDomainEvent(new EntityUpdatedDomainEvent<Category>(originJson, this));
        }
        public void UpdateCode(string code)
        {
            var originJson = JsonSerializer.Serialize(this);
            Code = code;
            AddDomainEvent(new EntityUpdatedDomainEvent<Category>(originJson, this));
        }
    }
}
